# -*- coding: utf-8 -*-
import unittest
import sys
import os
import time


# 添加执行环境
file_exec_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))  # NOQA
sys.path.insert(0, file_exec_path)  # NOQA
from framework_strategy.gopy.main.engine import EventEngine, Event  # NOQA
from framework_strategy.gopy.base.api import BaseData  # NOQA
from framework_strategy.gopy.base import BaseAPI  # NOQA
from framework_strategy.gopy.trader.engine import MainEngine, EmailEngine, LogEngine  # NOQA


def test_func_1(event: Event):
    print("[call] test_func_1 call :: ", event.event_type, event.event_data)


def common_func(event: Event):
    print("[call] common_func call :: ", event.event_type, event.event_data)


class TestGateway(BaseAPI):

    def __init__(self, event_engine: EventEngine, gateway_name: str) -> None:
        super().__init__(event_engine, gateway_name)

    def auth(self):
        pass


def init_engine():
    event_engine = EventEngine()
    main_engine = MainEngine(event_engine)
    return main_engine, event_engine


class TestEngine(unittest.TestCase):

    def test_main_engine_write_log(self):

        main_engine, event_engine = init_engine()

        res = main_engine.get_all_engines()
        print("[get_all_engines] ", res)

        # 测试异常日志
        tg = TestGateway(event_engine, "Test")
        main_engine.get_gateway(tg.gateway_name)
        print("[-]  exit ")

        main_engine.close()

    @unittest.skip
    def test_main_engine(self):

        event_engine = EventEngine()
        main_engine = MainEngine(event_engine)

        # 添加引擎
        main_engine.add_engine(EmailEngine)
        main_engine.add_engine(LogEngine)

        # view
        res = main_engine.get_all_engine_names()
        print("[get_all_engine_names] ", res)

        res = main_engine.get_all_engines()
        print("[get_all_engines] ", res)

    @unittest.skip
    def test_engine_init(self):

        eeg = EventEngine()
        eeg.start()
        print("[*] ", "start")
        eeg.stop()
        print("[*] ", "stop")

    @unittest.skip
    def test_engine_bind(self):

        eeg = EventEngine()
        eeg.start()
        print("[*] ", "start")

        eeg.on_bind("test_func_1", test_func_1)
        handles = eeg.handles()
        print("[*] handles -> ", handles)

        # call
        BaseData.gateway_name = "exchange"
        test_event = Event("test_func_1", BaseData)
        eeg.put(test_event)

        # 停顿一段时间
        time.sleep(3)
        # out
        eeg.out_bind("test_func_1", test_func_1)
        handles = eeg.handles()
        print("[*] handles -> ", handles)

        eeg.stop()
        print("[*] ", "stop")

    @unittest.skip
    def test_engine_run(self):
        eeg = EventEngine()
        eeg.start()
        print("[*] ", "start")

        eeg.on_bind("test_func_1", test_func_1)
        eeg.register_handle(common_func)
        handles = eeg.handles()

        print("[*] handles -> ", handles)
        general_handles = eeg.general_handles()
        print("[*] general_handles -> ", general_handles)

        # call
        BaseData.gateway_name = "exchange"
        test_event = Event("test_func_1", BaseData)
        eeg.put(test_event)

        # 停顿一段时间
        time.sleep(3)
        # out
        eeg.out_bind("test_func_1", test_func_1)
        eeg.unregister_handle(common_func)

        handles = eeg.handles()
        print("[*] handles -> ", handles)
        general_handles = eeg.general_handles()
        print("[*] general_handles -> ", general_handles)

        eeg.stop()
        print("[*] ", "stop")


if __name__ == "__main__":
    unittest.main()
